using System;

namespace EasyAdmin.Admin.Entity.Common
{
    /// <summary>
    /// API统一返回结果
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public object Data { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success => Code == 200;

        /// <summary>
        /// 成功返回
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiResult Ok(string message = "操作成功", object data = null)
        {
            return new ApiResult
            {
                Code = 200,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 失败返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        public static ApiResult Fail(string message, int code = 400)
        {
            return new ApiResult
            {
                Code = code,
                Message = message,
                Data = null
            };
        }

        /// <summary>
        /// 参数错误
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult BadRequest(string message = "参数错误")
        {
            return Fail(message, 400);
        }

        /// <summary>
        /// 未授权
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult Unauthorized(string message = "未授权")
        {
            return Fail(message, 401);
        }

        /// <summary>
        /// 权限不足
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult Forbidden(string message = "权限不足")
        {
            return Fail(message, 403);
        }

        /// <summary>
        /// 资源不存在
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult NotFound(string message = "资源不存在")
        {
            return Fail(message, 404);
        }

        /// <summary>
        /// 服务器错误
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult InternalServerError(string message = "服务器错误")
        {
            return Fail(message, 500);
        }
    }

    /// <summary>
    /// API统一返回结果（泛型）
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class ApiResult<T> : ApiResult
    {
        /// <summary>
        /// 数据
        /// </summary>
        public new T Data { get; set; }

        /// <summary>
        /// 成功返回
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public static ApiResult<T> Ok(T data, string message = "操作成功")
        {
            return new ApiResult<T>
            {
                Code = 200,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 失败返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        public static new ApiResult<T> Fail(string message, int code = 400)
        {
            return new ApiResult<T>
            {
                Code = code,
                Message = message,
                Data = default(T)
            };
        }

        /// <summary>
        /// 参数错误
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static new ApiResult<T> BadRequest(string message = "参数错误")
        {
            return Fail(message, 400);
        }

        /// <summary>
        /// 未授权
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static new ApiResult<T> Unauthorized(string message = "未授权")
        {
            return Fail(message, 401);
        }

        /// <summary>
        /// 权限不足
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static new ApiResult<T> Forbidden(string message = "权限不足")
        {
            return Fail(message, 403);
        }

        /// <summary>
        /// 资源不存在
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static new ApiResult<T> NotFound(string message = "资源不存在")
        {
            return Fail(message, 404);
        }

        /// <summary>
        /// 服务器错误
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static new ApiResult<T> InternalServerError(string message = "服务器错误")
        {
            return Fail(message, 500);
        }
    }
}